//
//  EnterPwd.m
//  MUBATE
//
//  Created by sunbin on 2017/2/20.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "EnterPwd.h"
#import "AccountOperation.h"

@interface EnterPwd ()
@property (weak, nonatomic) IBOutlet BanPasteTextField *enterPwd;
@property (weak, nonatomic) IBOutlet UITextField *pwd;

@end

@implementation EnterPwd
{UIButton*rightButton;}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];

}

- (void)initView{

    self.row = 1;
    
    self.title = LocalizedString(@"Enter Pwd");
    [self setRightItem];
    [_pwd leftViewModeWithConstrainedToWidth:100.f text:LocalizedString(@"enter pwd :") fon:App_textLeftViewFon setFont:App_textFon leftTextColor:[UIColor appLeftTextColor] bodyTextColor:[UIColor aPPBodyTextColor] PlaceholderColor:[UIColor appPlaceholderColor] setPlaceholder:LocalizedString(@"Please enter your login password") isLaunch:NO];
    
}

- (void)setRightItem{
    NSString *btnTitlt = LocalizedString(@"next step");
    CGFloat width = [btnTitlt widthWithFont:FONT_FONTMicrosoftYaHei(APP_confirmBtnFon) constrainedToHeight:44]+30.f;
    
    rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,width,44)];
    [rightButton setTitle:btnTitlt forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor appBtnStateNormalTitleColor] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor appBtnStateDisabledTitleColor] forState:UIControlStateDisabled];
     rightButton.enabled = NO;
    [rightButton addTarget:self action:@selector(nextStep)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    self.navigationItem.rightBarButtonItem= rightItem;

}

- (void)nextStep{
    WEAKSELF;
    [weakSelf.view endEditing:YES];
    NSString *logPwd = [[YConfig getOwnAccountAndPassword] lastObject];

    //模拟加载
    [weakSelf.navigationController.view showLoading:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf.navigationController.view hideHUD];
        //判断密码是否验证成功
        NSLog(@"==%@  ==%@" ,[weakSelf.pwd.text md5String],logPwd);
        if ([[weakSelf.pwd.text md5String] isEqualToString:logPwd]) {
        //成功
            [weakSelf inputCorrect];
        }else{
        //失败
            [weakSelf showPwdError];
        }
        
    });
    
    
}

// 密码输入正确
- (void)inputCorrect{
    WEAKSELF;
    AccountOperation *accountOperation = kVCFromSb(@"AccountOperationID", @"CorporateCenter");
    accountOperation.bingAccountType = weakSelf.bingAccountType;
    accountOperation.modifyMake = weakSelf.modifyMake;
    [weakSelf.navigationController pushViewController:accountOperation animated:NO];
}

- (void)showPwdError{
    

    //密码错误
    [NHUtils alertAction:@selector(clearPwdText) alertControllerWithTitle:LocalizedString(@"Error") Message:LocalizedString(@"Wrong password, please enter again") Vctl:self Cancel:NO];
    
}

- (void)clearPwdText{
     _pwd.text = nil;
    [_pwd becomeFirstResponder];
     rightButton.enabled = NO;
}
- (IBAction)editingChanged:(UITextField *)sender {
    rightButton.enabled = ![NHUtils isBlankString:sender.text];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if (![NHUtils isBlankString:_pwd.text]) {
        [self nextStep];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
